define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var xuiService = require("$UI/system/components/designerCommon/js/xuiService");
	
	var Model = function(){
		this.callParent();
		this.templateId = justep.Bind.observable();
		this.title = justep.Bind.observable();
		this.returnResult;
		this.groupId = "";
		this.c1View = justep.Bind.observable(false);
		this.c2View = justep.Bind.observable(true);
	};
	
	Model.prototype.groupsCustomerRefresh = function(event){
		var group = event.source;
		var append = event.options && event.options.append;
		var params = {
			"key" : "com.qq.weixin.miniapp.appID"
		};
		$.ajax({
			type : "GET",
			url : require.toUrl("/config/config/groups"),
			dataType : "json",
			async : false,// 使用同步方式，目前data组件有同步依赖
			cache : false,
			data : params,
			success : function(res) {
				group.loadData(res);
				group.loadData(res,append);
				group.doRefreshAfter(true, event.options);
			}
		});
	}

	Model.prototype.templatesCustomRefresh = function(event){
		var template = event.source;
		var append = event.options && event.options.append;
		var params = {
			"pageSize" : event.limit,
			"pageNum" : event.offset/event.limit + 1,
			"group":this.groupId
		};
		var size = event.limit;
		$.ajax({
			type : "GET",
			url : require.toUrl("/wxapi/miniapp/templates"),
			dataType : "json",
			async : false,// 使用同步方式，目前data组件有同步依赖
			cache : false,
			data : params,
			success : function(res) {
				template.loadData(res.data);
				template.loadData(res.data,append);
				template.doRefreshAfter(true, event.options);
				if(res.data.length<=size) {
					template.setTotal(res.data.length);
				}else {
					template.setTotal(25);
				}
			}
		});
	};
	
	Model.prototype.radio1Change = function(event) {
		var row = event.bindingContext.$object;
		var self = this;
		this.templateId.set(self.comp("TemplateMessage").getValue("templateId",row));
		this.title.set(self.comp("TemplateMessage").getValue("title",row));
	};

	Model.prototype.getReturnValue = function(event) {
		// var data = {
		// 	"templateId" : this.templateId.get()
		// };
		var data = {};
		data[this.returnResult] = this.title.get()+":"+this.templateId.get();
		console.log(data);
		return data;
	};

	Model.prototype.modelLoad = function(event) {
		var initData = xuiService.getPageParams();
		console.log(initData);
		this.returnResult = initData.propName;
	};
	
	Model.prototype.loadTemplateList = function(event){
		this.groupId = event.bindingContext.$object.val('fgroup');
		// var appid = event.bindingContext.$object.val('value');
		// if(!this.groupId || appid.length == 0) {
		// 	justep.Util.hint("当前选择小程序未配置appid等参数，请先配置！");
		// }
		this.c1View.set(true);
		this.c2View.set(false);
		this.comp('TemplateMessage').refreshData();
	};
	
	return Model;
});